<?php
namespace App\Http\Controllers;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends CommonController
{

    public function index()
    {
        dump('user1');
    }
    public function getAll(Request $request)  {


        // 使用示例
        $sortBy = [
            'created_at' => 'desc',
        ];
        $where = [
            'status' => 1,
        ];
        $type = $request->input('type',[1,2,3,4]);
        $select = ['id','name','type'];

        return getModelAllList(\App\Models\User::class, $sortBy, $where,$type,'type', $select);
    }
    /**
     * 列表获取
     * @param Request $request
     * @return false|string
     */
    public function getlist(Request $request)
    {
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $status = $request->input('status',null);
        $company_id = $request->input('company_id',null);
        $name = $request->input('name',null);
        $sortBy = $request->input('sortBy', ['created_at' => 'desc','status' => 'desc']);
        $model = new User();
        $query = $model->query();
        //定义排序查询
        foreach ($sortBy as $field => $direction) {
            $query->orderBy($field, $direction);
        }
        //状态条件
        if($status){
            $query->where('status','=',$status);
        }
        //单位条件
        if($company_id){
            $query->where('company_id','=',$company_id);
        }
        //多个查询
        if($name){
            $query->where('name','like', '%'.$name.'%');
            $query->orWhere('phone','like', '%'.$name.'%');
            $query->orWhere('username','like', '%'.$name.'%');
        }
        //查询用户关所在单位
        $query->with(['companyList' => function ($q) {
            $q->select('id', 'name');
        }]);
        //查询用户管理的角色
//        $query->with(['rolesList' => function ($q) {
//            $q->select('id', 'name');
//        }]);
        //分页查询
        $users = $query->paginate( $limit,['*'],'page',$page)->toArray();
        //返回对应格式
        $data = [
            "data"=>$users['data'] ,
            "total"=>$users['total'],
            "limit"=>$users['per_page'],
            "page"=>$users['current_page'],
            "last_page"=>$users['last_page']
        ];
        if($users['total'] == 0){
            return re_warning('暂无数据',null);
        }else{
            return re_success('操作成功',$data);
        }
    }
    public function getRoles($arr,$id)
    {


    }
    /**
     * 创建
     * @param Request $request
     * @return false|string
     */
    public function create(Request $request)
    {
        // 逻辑代码
        $data = $request->post();
        //密码加密
        $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['status'] = 1;
        //将手机号作为账号
        $data['username'] =  $data['phone'];
        $data['role_ids'] = json_encode($data['role_ids']);
        //创建数据
        $info = User::where('phone',$request->input('phone'))->exists();
        if($info){
            return  re_warning("手机号、账号{$request->input('phone')}已存在",null);
        }
        return insertData('phone',$data, User::class);

    }

    /**
     * 修改数据
     * @param Request $request
     * @return false|string
     */
    public function update(Request $request)
    {
        $data = $request->all();
        $data['username'] =  $data['phone'];
        //是否存在修改密码
        if($data['password'] &&  $data['password_check']){
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        }else{
            //不存在移除密码
            unset($data['password']);
        }
        $data['role_ids'] = json_encode($data['role_ids']);
        //不存在移除密码
        unset($data['password_check']);
        $data['updated_at'] = date('Y-m-d H:i:s');

        $info = User::where('phone',$data['phone'])->where('id','<>',$request->input('id'))->exists();
        if($info){
            return  re_warning("手机号、账号{$data['phone']}已存在",null);
        }
        return   updateData(User::class,$request->input('id'),$data);
    }

    /**
     * 修改状态
     * @param Request $request
     * @return void
     */
    public function status(Request $request)
    {
        $data = $request->all();
        $model = User::find($data['id']);
        $model->status = $model->status === 1 ? 2 : 1 ;
        $info =   $model->save();
        if ($info) {
            return  re_success("修改成功",null);
        } else {
            return  re_danger("修改失败",null);
        }


    }
    public function delete(Request $request){
        $id = $request->input('id');
        $info= User::destroy($id);
        if ($info) {
            return  re_success("删除成功",null);
        } else {
            return  re_danger("删除失败",null);
        }
    }

    public function info(Request $request)
    {
        $info = User::find($request->id);
        $info['role_ids'] = json_decode($info['role_ids']);
        if($info){
            return  re_success("添加成功",$info);
        }else{
            return  re_warning("没有该数据",null);

        }
    }

}
